<template>
  <div class="app">
    <div class="login">
      <div class="left">
        <div class="header">
          <img src="@/assets/login_logo.png" alt="" class="logo" />
          <span class="hmmm">黑马面面</span>
          <span class="a1"></span>
          <span class="user">用户登录</span>
        </div>
        <el-form ref="form" class="form" :model="form" :rules="rules">
          <el-form-item prop="phone">
            <el-input
              v-model.trim="form.phone"
              placeholder="请输入手机号"
              prefix-icon="el-icon-user"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              v-model.trim="form.password"
              placeholder="请输入密码"
              prefix-icon="el-icon-lock"
            ></el-input>
          </el-form-item>

          <el-row :gutter="10">
            <el-col :span="16">
              <el-form-item prop="code">
                <el-input
                  prefix-icon="el-icon-key"
                  v-model.trim="form.code"
                  placeholder="请输入4位验证码"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <img style="width:100%;height:40px;" :src="baseUrl+'/captcha?type=login&ttt='+num" alt=""  @click="num=Date.now()" />
            </el-col>
          </el-row>
          <el-form-item >
            <el-checkbox
              v-model="checked"
              prop="checked"
              label="我已阅读并同意"
              name="type"
              @click="checked=!checked"
            >
              我已阅读并同意
              <el-link type="primary">用户协议</el-link>
              和
              <el-link type="primary">隐私条款</el-link>
            </el-checkbox>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" class="btn" @click="submit"
              >登录</el-button
            >
          </el-form-item>
          <el-form-item>
            <el-button type="primary" class="btn" @click="submit2"
              >注册</el-button
            >
          </el-form-item>
        </el-form>
      </div>
      <img src="@/assets/login_banner_ele.png" alt="" />
    </div>
    <!-- 使用注册小组件 -->
    <my-register ref="register" />
  </div>
</template>

<script>
// 导入登录页面
import { Login } from '@/api/login'
import MyRegister from '@/views/MyRegister'
export default {
  name: 'MYLogin',
  components: {
    MyRegister
  },
  data () {
    return {
      checked: true,

      form: {
        phone: '18511111111',
        password: '12345678',
        code: ''

      },
      baseUrl: process.env.VUE_APP_URL,
      num: Date.now(),
      rules: {
        phone: [
          { required: true, message: '请输入手机号', trigger: 'change' },
          { pattern: /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/, message: '请输入正确的手机号', trigger: 'change' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'change' },
          { min: 6, max: 12, message: '请输入6-12位密码', trigger: 'change' }
        ],
        code: [
          { required: true, message: '请输入验证码', trigger: 'change' },
          { min: 4, max: 5, message: '请输入正确验证码', trigger: 'change' }
        ]

      }
    }
  },
  methods: {
    // 登录点击事件
    submit () {
    // 表单验证
      this.$refs.form.validate(async result => {
        console.log(result)
        if (this.checked) {
          if (result) {
            const res = await Login(this.form)
            console.log('登录成功', res)

            // 保存token到vuex
            this.$store.commit('user/SETTOKEN', res.data.token)
            this.$store.dispatch('user/setinfo')
            // 清空表单
            this.$refs.form.resetFields()

            if (res.code === 200) {
              this.$message.success('登录成功')
              // 跳转页面到首页
              this.$router.push('/')
            } else {
              this.$message.error(res.message)
              this.num = Date.now()
            }
          }
        } else {
          this.$message.error('请勾选相关协议')
        }
      })
    },

    // 注册点击事件
    submit2 () {
      this.$refs.register.isShow = true
    }
  }
}
</script>

<style lang="scss" scoped>
* {
  padding: 0;
  margin: 0;
  box-sizing: border-box;
  list-style: none;
}
.login {
  // margin-top: 100px;
  height: 100vh;
  background: linear-gradient(#1493fa 28%, #01c6fa 96%);
  display: flex;
  align-items: center;
  justify-content: space-around;
  .left {
    // margin-top: 80px;
    width: 478px;
    height: 550px;
    background-color: #f5f5f5;
    padding: 44px 42px;
    .header {
      width: 394px;
      height: 32px;
      // background-color: rgb(217, 233, 186);
      box-sizing: border-box;
      display: flex;
      align-items: center;
      .logo {
        width: 25px;
      }
      .hmmm {
        font-size: 24px;
        font-family: SourceHanSansCN Regular, SourceHanSansCN Regular-Regular;
        font-weight: 400;
        text-align: left;
        color: #0c0c0c;
        margin-left: 13px;
        margin-right: 13px;
      }
      .a1 {
        width: 1px;
        height: 28px;
        background: #c7c7c7;
        margin-right: 13px;
      }
      .user {
        font-size: 22px;
        font-family: PingFangSC Regular, PingFangSC Regular-Regular;
        font-weight: 400;
        text-align: left;
        color: #0c0c0c;
      }
    }
    .code-img {
      width: 100%;
      height: 40px;
    }
    .form {
      margin-top: 31px;
      .el-form-item {
        margin-bottom: 22px;
        ::v-deep .el-form-item__content {
          display: flex;
          font-size: 14px;
          .img {
            width: 131px;
            height: 40px;
            background-color: #f3fbfe;
            img {
              width: 100%;
              height: 100%;
              object-fit: contain;
            }
          }
        }
        .btn {
          width: 100%;
        }
      }
    }
  }
}
</style>
